# Create your views here.
"""
this file seems to be too big, need split
"""

import urllib
import logging

from django.http import HttpResponse
from django.shortcuts import render_to_response,redirect
from google.appengine.api import memcache

from LyricStructure.pylyric.Lyric import Lyric
from lyricemo.utils import split_by_emoword
from lyricemo.utils import get_emodict,get_pattern_list,get_c2wlist,get_split_list
from lyricsplit.models import LyricLrc
from lyricemo.models import EmoWordAnnoOk



def home(request):
    # show some description
    if request.method == "GET":
        return render_to_response('lyricemo/index.html',{})
    else:
        from musiccrawler.baidu.baidusearch import search_music
        k = request.POST['k']
        if k.startswith('mid'):
            # get m_list
            k = k[4:]
            k2 = k[:-1]+str(int(k[-1])+1)
            ll_list = LyricLrc.all().filter("name >=",k).filter("name <=",k2)
            m_list = []
            for ll in ll_list:
                tmp_dic = {}
                tmp_dic['k'] = ll.key()
                tmp_dic['name'] = ll.name
                m_list.append(tmp_dic)

            dic = {}
            dic['local'] = 1
            dic['m_list'] = m_list
            return render_to_response('lyricemo/index.html',dic)
        else:
            m_list = search_music(k)
            dic = {}
            dic['baidu'] = 1
            dic['m_list'] = m_list
            return render_to_response('lyricemo/index.html',dic)


def wordemo(request):
    # check the emo annotation
    # TODO  merge the three function of content annotation
    dic = {}
    if request.method == "GET":
        return render_to_response('lyricemo/wordemo.html',dic)
    else:
        lrcstr = request.POST['LrcStr']
        dic['textareavalue'] = lrcstr
        dic['split_list'] = get_split_list(lrcstr)
        return render_to_response('lyricemo/wordemo.html',dic)
            
def baiduemo(request):
    # TODO  merge the three function of content annotation
    bid = request.GET['bid']
    from musiccrawler.baidu.baidulyric import craw_lrc_by_mid
    lrcstr = craw_lrc_by_mid(bid)
    lrcstr = lrcstr.decode('utf-8')
    dic = {}
    dic['textareavalue'] = lrcstr
    dic['split_list'] = get_split_list(lrcstr)
    return render_to_response('lyricemo/wordemo.html',dic)

def localemo(request):
    k = request.GET['k']
    ll = LyricLrc.get(k)
    lrcstr = ll.lrc_str
    dic = {}
    dic['textareavalue'] = lrcstr
    dic['split_list'] = get_split_list(lrcstr)
    dic['mkey'] = k
    dic['ep_list'] = get_pattern_list(k)

    return render_to_response('lyricemo/wordemo.html',dic)


def force_update_emow(request):
    """
    this should be a cron request..
    """
    # TODO move to the cron task
    from lyricemo.utils import force_update_emodict
    force_update_emodict()
    return HttpResponse('ok')


def anno_emo_word_page(request):
    """
    redirect to the page of localemo
    """
    #return render_to_response('lyricemo/wordemo.html',dic)
    if EmoWordAnnoOk.all().filter('anno_ok =',0).count() == 0:
        return HttpResponse('no left for anno')

    ewao = EmoWordAnnoOk.all().filter('anno_ok =',0)[0]
    ewao.anno_ok = 1
    ewao.put()

    lrc = ewao.lrc
    return redirect( '/lyricemo/local?k=%s'%( str(lrc.key()) ) )

